import pageJson from "@/pages.json"
export const tip = function (title: string, fun?: Function, delay = 1000) {				//普通提示
	uni.showToast({
		title: title,
		icon: 'none',
		duration: delay,
		mask: true
	});
	if (typeof fun == "function") {
		setTimeout(function () {
			fun();
		}, delay);
	}
}

export const goback = function () {			//返回
	if (isFirst()) {
		goto(pageJson.pages[0].path.replace('pages/', ''), '', 0, true)			//pages里的第一个页面
	}
	else {
		uni.navigateBack()
	}
}
export const isFirst = () => {			//打开的第一个页面
	let pages = getCurrentPages()
	if (pages.length == 1) {
		return true
	}
	else {
		return false
	}
}
export const loading = function (title: string) {			//加载中
	uni.showLoading({
		title: title,
		mask: true
	})
}
export const hideloading = function () {
	uni.hideLoading()
}
//跳转页面
//path：路径 subPackage：分包，空表示主包
export const goto = function (path: string, subPackage = "", is_recirect = 0, is_tabbar = false) {
	// var is_tabbar = false
	if (subPackage) {
		path = "/" + subPackage + "/pages/" + path;
	}
	else {
		//是否是tabbar页面
		path = "pages/" + path;
		is_tabbar = pageJson.tabBar.list.some((page: { pagePath: string; }) => page.pagePath === path);
		path = "/" + path
	}
	if (is_tabbar) {
		uni.switchTab({
			url: path,
			fail: e => {
				console.error(e);
			}

		})
	}
	else {
		if (is_recirect == 1) {
			uni.redirectTo({
				url: path,
				fail: e => {
					console.error(e);
				}
			});
		}
		else {
			uni.navigateTo({
				url: path,
				fail: e => {
					console.error(e);
				}
			})
		}
	}
}

//富文本替换
export const richtxt = function (content: string) {
	const regex = /<img([^>]*)>/gi;
	content = content.replace(regex, (match, attributes) => {
		if (attributes.includes('style=')) {
			return match.replace(/style=[\'"][^\'"]*[\'"]/gi, 'style="max-width: 100%;display:block"');
		} else {
			return match.replace('<img', '<img style="max-width: 100%;display:block"');
		}
	});
	return content
}

export const datetime = function (time = "", timeformat = "Y-m-d H:i:s") {
	let date = new Date()
	if (time != "") {
		date = new Date(time)
	}
	let Y: number = date.getFullYear()
	let m: number = date.getMonth() + 1
	let mstr: string = m < 10 ? "0" + m : m.toString()
	let d: number = date.getDate()
	let dstr: string = d < 10 ? "0" + d : d.toString()
	let H: number = date.getHours()
	let Hstr: string = H < 10 ? "0" + H : H.toString()
	let i: number = date.getMinutes()
	let istr: string = i < 10 ? "0" + i : i.toString()
	let s: number = date.getSeconds()
	let sstr: string = s < 10 ? "0" + s : s.toString()
	let str = timeformat.replace("Y", Y.toString())
	str = str.replace("m", mstr)
	str = str.replace("d", dstr)
	str = str.replace("H", Hstr)
	str = str.replace("i", istr)
	str = str.replace("s", sstr)
	return str
}
export const copy = function (con: string, tip = "复制成功", callback?: Function) {
	uni.setClipboardData({
		data: con,
		showToast: tip != '',
		success() {
			if (tip != '') {
				uni.showToast({
					title: tip,
					icon: 'none',
					duration: 2000,
					mask: true
				});
			}
			if (typeof callback == "function") {
				callback()
			}
		}
	})
}

export const confirm = function (opt: any, done: Function, cancel?: Function) {
	let option = {
		title: '提示',
		content: "",
		success(res: any) {
			if (res.confirm) {
				if (typeof done == "function") {
					done()
				}
			}
			else {
				if (typeof cancel == "function") {
					cancel()
				}
			}
		}
	}
	if (typeof opt == "object") {
		Object.assign(option, opt)
	}
	else if (typeof opt == "string") {
		option.content = opt
	}
	uni.showModal(option)
}
//下载图片
export const downImg = function (file_url: string) {
	loading("正在保存")
	uni.downloadFile({		//下载图片
		url: file_url,
		filePath: '',
		success(res: any) {
			uni.saveImageToPhotosAlbum({
				filePath: res.tempFilePath,
				success() {
					hideloading()
					tip("保存成功！")
				},
				fail(err: any) {
					console.log(err)
					hideloading()
					tip("保存失败")
				}
			})
		},
		fail() {
			hideloading()
			tip("保存失败")
		}
	})
}

//下载文件
export const downVideo = function (video_url: string) {
	loading("正在保存")
	uni.downloadFile({		//下载图片
		url: video_url,
		filePath: '',
		success(res: any) {
			uni.saveVideoToPhotosAlbum({
				filePath: res.tempFilePath,
				success() {
					hideloading()
					tip("保存成功！")
				},
				fail(err: any) {
					console.log(err)
					hideloading()
					tip("保存失败")
				}
			})
		},
		fail() {
			hideloading()
			tip("保存失败")
		}
	})
}

export const tel = (e: string) => {
	uni.makePhoneCall({
		phoneNumber: e
	})
}

//是否是手机号
export const isMobile = (e: string) => {
	var pattern = /^1[3456789]\d{9}$/;
	return pattern.test(e);
}

//检测登录
export const isLogin = () => {
	let token = uni.getStorageSync('token')
	if (token) {
		return true
	}
	else {
		return false
	}
}